package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import model.NewsBean;
import utils.DBUtils;

public class NewsDao {

	public List<NewsBean> query_all_news() {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from news";
		ArrayList<NewsBean> results = new ArrayList<NewsBean>();
		
		try {
			PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();

			while(rs.next()){
				
				NewsBean temp = new NewsBean();
				temp.setId(rs.getString("id"));
				temp.setTitle(rs.getString("title"));
				temp.setConetnt(rs.getString("conetnt"));
				temp.setAuthor(rs.getString("author"));
				temp.setAddtime(rs.getDate("addtime"));
				results.add(temp);
			}
			rs.close();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtils.closeConnection(conn);
		}
		return results;
	}
	
	public NewsBean getNews(String id) {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from news where id='"+id+"' ";
		NewsBean result = new NewsBean();
		
		try {
			PreparedStatement ps = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();

			while(rs.next()){

				result.setId(rs.getString("id"));
				result.setTitle(rs.getString("title"));
				result.setConetnt(rs.getString("conetnt"));
				result.setAuthor(rs.getString("author"));
				result.setAddtime(rs.getDate("addtime"));
			}

			rs.close();
			ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtils.closeConnection(conn);
		}
		return result;
	}
	
	
	
}
